<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="GUI框架介绍Java GUI 总体框架包括两个主要的部分：AWT（Abstract Window Toolkit）和 Swing。AWT 是 Java 原生的 GUI 库，而 Swing 是在 AWT 的基础上开发的一个更高级的 GUI 库。下面分别介绍一下 AWT 和 Swing 的特点和各自负责的部分。 AWTAWT 是 Java 最早的 GUI 库，它提供了一套基本的 GUI 组件，如窗口">
<meta property="og:type" content="article">
<meta property="og:title" content="Java：GUI编程">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="GUI框架介绍Java GUI 总体框架包括两个主要的部分：AWT（Abstract Window Toolkit）和 Swing。AWT 是 Java 原生的 GUI 库，而 Swing 是在 AWT 的基础上开发的一个更高级的 GUI 库。下面分别介绍一下 AWT 和 Swing 的特点和各自负责的部分。 AWTAWT 是 Java 最早的 GUI 库，它提供了一套基本的 GUI 组件，如窗口">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiaoqingming18.github.io/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/框架图.png">
<meta property="article:published_time" content="2023-05-20T02:52:23.000Z">
<meta property="article:modified_time" content="2023-06-28T23:59:30.746Z">
<meta property="article:author" content="鸣蜩十七">
<meta property="article:tag" content="-Java基础 -GUI">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiaoqingming18.github.io/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/框架图.png">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/","path":"2023/05/20/Java：GUI编程/","title":"Java：GUI编程"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java：GUI编程 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#GUI%E6%A1%86%E6%9E%B6%E4%BB%8B%E7%BB%8D"><span class="nav-text">GUI框架介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#AWT"><span class="nav-text">AWT</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Swing"><span class="nav-text">Swing</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Swing%E6%A1%86%E6%9E%B6%E5%9B%BE"><span class="nav-text">Swing框架图</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%B9%E5%99%A8%E4%B8%8E%E7%BB%84%E4%BB%B6"><span class="nav-text">容器与组件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%B9%E5%99%A8"><span class="nav-text">容器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%84%E4%BB%B6"><span class="nav-text">组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%91%E5%AE%B9%E5%99%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E7%BB%84%E4%BB%B6"><span class="nav-text">向容器中添加组件</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AA%97%E5%8F%A3"><span class="nav-text">窗口</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#JFrame"><span class="nav-text">JFrame</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8F%9C%E5%8D%95%E6%9D%A1%E3%80%81%E8%8F%9C%E5%8D%95%E3%80%81%E8%8F%9C%E5%8D%95%E9%A1%B9"><span class="nav-text">菜单条、菜单、菜单项</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#JMenuBar"><span class="nav-text">JMenuBar</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JMenu"><span class="nav-text">JMenu</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JMenuItem"><span class="nav-text">JMenuItem</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6"><span class="nav-text">常用组件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E5%AE%B9%E5%99%A8"><span class="nav-text">常用容器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#JPanel%EF%BC%88%E9%9D%A2%E6%9D%BF%EF%BC%89"><span class="nav-text">JPanel（面板）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JTabbedPane"><span class="nav-text">JTabbedPane</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JScrollPane%EF%BC%88%E6%BB%9A%E5%8A%A8%E7%AA%97%E6%A0%BC%EF%BC%89"><span class="nav-text">JScrollPane（滚动窗格）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSplitPane%EF%BC%88%E6%8B%86%E5%88%86%E7%AA%97%E6%A0%BC%EF%BC%89"><span class="nav-text">JSplitPane（拆分窗格）</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E7%BB%A7%E6%89%BFJFrame%E7%BB%93%E6%9E%84"><span class="nav-text">使用继承JFrame结构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8"><span class="nav-text">布局管理器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8%E4%BB%8B%E7%BB%8D"><span class="nav-text">布局管理器介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8"><span class="nav-text">如何使用布局管理器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#BorderLayout%EF%BC%88%E8%BE%B9%E7%95%8C%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8%EF%BC%89%E4%BD%BF%E7%94%A8%E4%B8%BE%E4%BE%8B"><span class="nav-text">BorderLayout（边界布局管理器）使用举例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#FlowLayout%EF%BC%88%E6%B5%81%E5%BC%8F%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8%EF%BC%89%E4%BD%BF%E7%94%A8%E4%B8%BE%E4%BE%8B"><span class="nav-text">FlowLayout（流式布局管理器）使用举例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GridLayout%EF%BC%88%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8%EF%BC%89%E4%BD%BF%E7%94%A8%E4%B8%BE%E4%BE%8B"><span class="nav-text">GridLayout（网格布局管理器）使用举例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CardLayout%EF%BC%88%E5%8D%A1%E7%89%87%E5%B8%83%E5%B1%80%E7%AE%A1%E7%90%86%E5%99%A8%EF%BC%89%E4%BD%BF%E7%94%A8%E4%B8%BE%E4%BE%8B"><span class="nav-text">CardLayout（卡片布局管理器）使用举例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#GUI%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84%E7%B1%BB"><span class="nav-text">GUI中常用的类</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Color%E7%B1%BB"><span class="nav-text">Color类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E9%A2%84%E5%AE%9A%E4%B9%89%E9%A2%9C%E8%89%B2%E5%B8%B8%E9%87%8F"><span class="nav-text">使用预定义颜色常量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E8%87%AA%E5%AE%9A%E4%B9%89%E9%A2%9C%E8%89%B2"><span class="nav-text">创建自定义颜色</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Font%E7%B1%BB"><span class="nav-text">Font类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ImageIcon%E7%B1%BB"><span class="nav-text">ImageIcon类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BAImageIcon%E5%AF%B9%E8%B1%A1"><span class="nav-text">创建ImageIcon对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E5%9B%BE%E5%83%8F%E7%9A%84%E5%AE%BD%E5%BA%A6%E5%92%8C%E9%AB%98%E5%BA%A6"><span class="nav-text">获取图像的宽度和高度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E5%9B%BE%E6%A0%87%E7%9A%84%E6%8F%8F%E8%BF%B0%E6%96%87%E6%9C%AC"><span class="nav-text">设置图标的描述文本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E5%9B%BE%E6%A0%87%E7%9A%84%E5%9B%BE%E5%83%8F"><span class="nav-text">获取图标的图像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#IconImage%E7%B1%BB%E5%9C%A8GUI%E7%BC%96%E7%A8%8B%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8"><span class="nav-text">IconImage类在GUI编程中的应用</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6%E5%A4%84%E7%90%86"><span class="nav-text">事件处理</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java：GUI编程 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java：GUI编程
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-20 10:52:23" itemprop="dateCreated datePublished" datetime="2023-05-20T10:52:23+08:00">2023-05-20</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-06-29 07:59:30" itemprop="dateModified" datetime="2023-06-29T07:59:30+08:00">2023-06-29</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">-后端</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="GUI框架介绍"><a href="#GUI框架介绍" class="headerlink" title="GUI框架介绍"></a>GUI框架介绍</h1><p>Java GUI 总体框架包括两个主要的部分：AWT（Abstract Window Toolkit）和 Swing。AWT 是 Java 原生的 GUI 库，而 Swing 是在 AWT 的基础上开发的一个更高级的 GUI 库。下面分别介绍一下 AWT 和 Swing 的特点和各自负责的部分。</p>
<h2 id="AWT"><a href="#AWT" class="headerlink" title="AWT"></a>AWT</h2><p>AWT 是 Java 最早的 GUI 库，它提供了一套基本的 GUI 组件，如窗口、按钮、文本框、复选框、列表框等。这些组件是使用本地窗口系统（如 Windows、Mac OS X 或 Linux）提供的原生控件实现的，因此<strong>AWT应用程序在不同的平台上看起来会有些许不同</strong>。AWT 是 Java 标准库的一部分，因此它可以在任何支持 Java 的平台上运行。</p>
<p>在 AWT 中，组件都是继承自 java.awt.Component 类的。AWT 的布局管理器（LayoutManager）负责组件的布局和排列，常用的布局管理器包括 BorderLayout、FlowLayout、GridLayout 等。</p>
<h2 id="Swing"><a href="#Swing" class="headerlink" title="Swing"></a>Swing</h2><p>Swing 是在 AWT 的基础上开发的一个更高级的 GUI 库，它提供了一套丰富的 GUI 组件和更强大的功能，如表格、树形控件、滚动条等。Swing 的组件是使用 Java 代码实现的，而不是使用本地窗口系统提供的原生控件，因此 <strong>Swing 应用程序在不同的平台上看起来完全一样</strong>。Swing 是 Java 的标准库之一，因此它可以在任何支持 Java 的平台上运行。</p>
<p>Swing 的主要特点包括：</p>
<ul>
<li>跨平台性：Swing 应用程序可以在任何支持 Java 的平台上运行，因为它使用了 Java 代码实现的 GUI 组件，而不是本地窗口系统提供的原生控件。</li>
<li>功能丰富：Swing 提供了一套丰富的 GUI 组件和更强大的功能，可以满足复杂 GUI 应用程序的需求。</li>
<li>可定制性：Swing 组件可以通过修改外观和行为来实现定制化，如修改组件的背景颜色、字体、样式等。</li>
<li>性能较高：Swing 的性能相对较高，因为它使用了 Java 代码实现的 GUI 组件，可以在处理复杂界面时提供更好的性能。</li>
<li>复杂度高：Swing 的开发和使用相对 AWT 来说更加复杂，因为它提供了更多的功能和可定制性，需要更多的学习和实践才能掌握。</li>
</ul>
<p>在 Swing 中，组件都是继承自 javax.swing.JComponent 类的。Swing 的布局管理器和 AWT 类似，也负责组件的布局和排列，常用的布局管理器包括 BorderLayout、FlowLayout、GridLayout、GridBagLayout 等。除此之外，Swing 还提供了更高级的布局管理器，如 BoxLayout、CardLayout等。</p>
<h2 id="Swing框架图"><a href="#Swing框架图" class="headerlink" title="Swing框架图"></a>Swing框架图</h2><p>下面我们通过一张图来介绍了解一下Swing有哪些组件：</p>
<p><img src="/2023/05/20/Java%EF%BC%9AGUI%E7%BC%96%E7%A8%8B/框架图.png" alt="框架图"></p>
<h1 id="容器与组件"><a href="#容器与组件" class="headerlink" title="容器与组件"></a>容器与组件</h1><h2 id="容器"><a href="#容器" class="headerlink" title="容器"></a>容器</h2><p>在 Java GUI 中，容器是一个可以包含其他组件的区域，它可以是顶级容器（如 JFrame、JDialog 等），也可以是内部容器（如 JPanel、JScrollPane 等）。容器可以嵌套，也就是说一个容器可以包含另一个容器，从而形成复杂的 GUI 布局。</p>
<p>常用的容器有：</p>
<ul>
<li>JFrame：顶级容器，代表一个窗口。</li>
<li>JDialog：顶级容器，代表一个对话框。</li>
<li>JPanel：内部容器，可以用来组织其他组件。</li>
<li>JScrollPane：内部容器，可以包含一个可滚动的视图。</li>
<li>JTabbedPane：内部容器，可以包含多个选项卡，每个选项卡可以显示不同的内容。</li>
</ul>
<h2 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h2><p>在 Java GUI 中，组件是一个可以在容器中显示的 GUI 元素，它可以是按钮、文本框、标签、列表框、滚动条等。组件可以接收用户的输入，也可以显示程序的输出。</p>
<h2 id="向容器中添加组件"><a href="#向容器中添加组件" class="headerlink" title="向容器中添加组件"></a>向容器中添加组件</h2><p>可以通过容器的<code>add(Component c)</code>方法向容器中添加组件，看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JPanel</span> <span class="variable">panel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(); <span class="comment">// 创建一个 JPanel</span></span><br><span class="line"><span class="type">JButton</span> <span class="variable">button</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Click Me"</span>); <span class="comment">// 创建一个 JButton</span></span><br><span class="line">panel.add(button); <span class="comment">// 将 JButton 添加到 JPanel 中</span></span><br></pre></td></tr></tbody></table></figure>
<h1 id="窗口"><a href="#窗口" class="headerlink" title="窗口"></a>窗口</h1><p>窗口是一个重要的概念，它是用户与程序交互的主要方式之一。Java 提供了多种窗口组件，如<code>JFrame</code>、<code>JDialog</code>、<code>JOptionPane</code> 等。这里先介绍<code>JFrame</code>的窗口操作。</p>
<h2 id="JFrame"><a href="#JFrame" class="headerlink" title="JFrame"></a>JFrame</h2><p>JFrame 是 Java GUI 中最常用的窗口组件，它代表一个顶级窗口，用于显示 GUI 应用程序的主界面。</p>
<p>JFrame 的构造方法如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">JFrame() <span class="comment">// 创建一个默认的 JFrame，其标题为 ""</span></span><br><span class="line">JFrame(String title) <span class="comment">// 创建一个指定标题的 JFrame</span></span><br></pre></td></tr></tbody></table></figure>
<p>JFrame 的常用方法如下：</p>
<ul>
<li>setTitle(String title)：设置 JFrame 的标题。</li>
<li>setSize(int width, int height)：设置 JFrame 的大小。</li>
<li>setLocation(int x, int y)：设置 JFrame 的位置。</li>
<li>setResizable(boolean resizable)：设置 JFrame 是否可调整大小，默认可调整大小。</li>
<li>add(Component c)：向 JFrame 中添加一个组件。</li>
<li>setVisible(boolean visible)：显示或隐藏 JFrame，<strong>默认是隐藏的，创建好窗口需要手动设置窗口为可见</strong>。</li>
<li>getContentPane()：返回 JFrame 的内容面板，可以向内容面板中添加其他组件。</li>
<li>setJMenuBar(JMenuBar menuBar)：设置 JFrame 的菜单栏。</li>
<li>setLayout(LayoutManager manager)：设置 JFrame 的布局管理器。</li>
<li>setIconImage(Image image)：设置 JFrame 的图标。</li>
<li>setDefaultCloseOperation(int operation)：用于设置窗口关闭时的操作。该方法接受一个整数参数，用于指定关闭时的操作，常用的参数值包括：（<strong>默认为HIDE_ON_CLOSE，即隐藏窗口</strong>）<ul>
<li>EXIT_ON_CLOSE：退出程序。当用户关闭窗口时，程序将终止。</li>
<li>DISPOSE_ON_CLOSE：释放资源。当用户关闭窗口时，窗口将被销毁，但程序将继续运行。</li>
<li>DO_NOTHING_ON_CLOSE：什么都不做。当用户关闭窗口时，不执行任何操作。</li>
</ul>
</li>
</ul>
<h2 id="菜单条、菜单、菜单项"><a href="#菜单条、菜单、菜单项" class="headerlink" title="菜单条、菜单、菜单项"></a>菜单条、菜单、菜单项</h2><p>菜单条、菜单、菜单项是窗口常用的组件，菜单放在菜单条里，菜单项放在菜单里。下面分别介绍一下这些组件的特点和用法。</p>
<h3 id="JMenuBar"><a href="#JMenuBar" class="headerlink" title="JMenuBar"></a>JMenuBar</h3><p>JMenuBar 是 Java GUI 中的菜单条组件，用于显示菜单和子菜单。它通常位于窗口的顶部，可以包含多个 JMenu 组件。</p>
<p>JMenuBar 的用法如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JMenuBar</span> <span class="variable">menuBar</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenuBar</span>(); <span class="comment">// 创建一个 JMenuBar</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建一个 JMenu，并添加到 JMenuBar 中</span></span><br><span class="line"><span class="type">JMenu</span> <span class="variable">fileMenu</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenu</span>(<span class="string">"File"</span>);</span><br><span class="line">menuBar.add(fileMenu);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建一个 JMenuItem，并添加到 JMenu 中</span></span><br><span class="line"><span class="type">JMenuItem</span> <span class="variable">openItem</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenuItem</span>(<span class="string">"Open"</span>);</span><br><span class="line">fileMenu.add(openItem);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将 JMenuBar 设置为 JFrame 的菜单栏</span></span><br><span class="line">frame.setJMenuBar(menuBar);</span><br></pre></td></tr></tbody></table></figure>
<h3 id="JMenu"><a href="#JMenu" class="headerlink" title="JMenu"></a>JMenu</h3><p>JMenu 是 Java GUI 中的菜单组件，用于显示菜单和子菜单。它通常作为 JMenuBar 的子组件，可以包含多个 JMenuItem 组件。用法如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JMenu</span> <span class="variable">fileMenu</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenu</span>(<span class="string">"File"</span>); <span class="comment">// 创建一个 JMenu</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建多个 JMenuItem，并添加到 JMenu 中</span></span><br><span class="line"><span class="type">JMenuItem</span> <span class="variable">openItem</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenuItem</span>(<span class="string">"Open"</span>);</span><br><span class="line"><span class="type">JMenuItem</span> <span class="variable">saveItem</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenuItem</span>(<span class="string">"Save"</span>);</span><br><span class="line">fileMenu.add(openItem);</span><br><span class="line">fileMenu.add(saveItem);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将 JMenu 添加到 JMenuBar 中</span></span><br><span class="line">menuBar.add(fileMenu);</span><br></pre></td></tr></tbody></table></figure>
<p>以上代码创建了一个 JMenu，并向其中添加了多个 JMenuItem。最后将 JMenu 添加到 JMenuBar 中。</p>
<h3 id="JMenuItem"><a href="#JMenuItem" class="headerlink" title="JMenuItem"></a>JMenuItem</h3><p>JMenuItem 是 Java GUI 中的菜单项组件，用于表示菜单和子菜单中的一个选项。它通常作为 JMenu 的子组件，可以响应用户的鼠标单击事件。看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JMenuItem</span> <span class="variable">openItem</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenuItem</span>(<span class="string">"Open"</span>); <span class="comment">// 创建一个 JMenuItem</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 将 JMenuItem 添加到 JMenu 中</span></span><br><span class="line">fileMenu.add(openItem);</span><br></pre></td></tr></tbody></table></figure>
<h1 id="常用组件"><a href="#常用组件" class="headerlink" title="常用组件"></a>常用组件</h1><p>常用组件都是<code>JComponent</code>的子类。</p>
<ol>
<li><code>JTextField</code>：文本框，允许用户在文本框中输入<strong>单行文本</strong>。</li>
<li><code>JTextArea</code>：文本区，允许用户在文本区输入<strong>多行文本</strong>。</li>
<li><code>JButton</code>：按钮，允许用户单击按钮。</li>
<li><code>JLabel</code>：标签，为用户提供提示信息。</li>
<li><code>JCheckBox</code>：复选框，为用户提供<strong>多项选择</strong>。</li>
<li><code>JRadioButton</code>：单选按钮，为用户提供<strong>单项选择</strong>。（如何实现单选）</li>
<li><code>JComboBox</code>：下拉列表，为用户提供下拉列表形式的<strong>单项选择</strong>。（菱形语法选择Item类型，使用addItem加入元素）</li>
<li><code>JPasswordField</code>：密码框，允许用户在密码框中输入单行密码，密码的默认回显字符是’ * ‘，可以使用<code>setEchoChar(char c)</code>方法设置回显字符，用户还可以通过<code>char[] getPassword()</code>方法获取用户输入的密码。</li>
</ol>
<h1 id="常用容器"><a href="#常用容器" class="headerlink" title="常用容器"></a>常用容器</h1><p><code>JComponent</code>是<code>Container</code>的子类，因此<code>JComponent</code>子类创建的组件也是容器，但是很少将组件当做容器来使用。<code>JComponent</code>提供了一些专门用来做容器的组件，相对于底层容器<code>JFrame</code>，本节提到的容器习惯地被称为<strong>中间容器</strong>，中间容器必须被添加到底层容器中才能发挥作用。</p>
<h2 id="JPanel（面板）"><a href="#JPanel（面板）" class="headerlink" title="JPanel（面板）"></a>JPanel（面板）</h2><p><code>JPanel</code>可以包含其他GUI组件，如按钮、文本框、标签等它不具备窗口装饰和其他窗口特性，因此常常被用作其他容器（如 JFrame、JDialog）中的子容器，用于布置和组织其他组件。JPanel 的用法非常简单，只需要创建一个 JPanel 对象，然后向其中添加其他组件即可。看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyPanel</span> <span class="keyword">extends</span> <span class="title class_">JPanel</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">MyPanel</span><span class="params">()</span> {</span><br><span class="line">        <span class="type">JButton</span> <span class="variable">button1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Button 1"</span>); <span class="comment">// 创建一个 JButton</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">button2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Button 2"</span>); <span class="comment">// 创建另一个 JButton</span></span><br><span class="line"></span><br><span class="line">        add(button1); <span class="comment">// 向 JPanel 中添加 JButton</span></span><br><span class="line">        add(button2); <span class="comment">// 向 JPanel 中添加另一个 JButton</span></span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">frame</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>(<span class="string">"My Frame"</span>); <span class="comment">// 创建一个 JFrame</span></span><br><span class="line"></span><br><span class="line">        <span class="type">MyPanel</span> <span class="variable">panel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyPanel</span>(); <span class="comment">// 创建一个 MyPanel</span></span><br><span class="line">        frame.add(panel); <span class="comment">// 将 MyPanel 添加到 JFrame 中</span></span><br><span class="line"></span><br><span class="line">        frame.setSize(<span class="number">400</span>, <span class="number">300</span>); <span class="comment">// 设置 JFrame 的大小</span></span><br><span class="line">        frame.setLocationRelativeTo(<span class="literal">null</span>); <span class="comment">// 将 JFrame 居中显示</span></span><br><span class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); <span class="comment">// 设置 JFrame 关闭时的操作为退出程序</span></span><br><span class="line">        frame.setVisible(<span class="literal">true</span>); <span class="comment">// 显示 JFrame</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="JTabbedPane"><a href="#JTabbedPane" class="headerlink" title="JTabbedPane"></a>JTabbedPane</h2><p><code>JTabbedPane</code>是一个选项卡组件，用于在同一个界面中切换不同的选项卡，每个选项卡可以包含不同的 GUI 组件。JTabbedPane通常用于显示不同的视图或功能模块，使用户可以快速地切换不同的页面。</p>
<p><code>JTabbedPane</code>还有个有参构造器<code>public JTabbedPane(int tabPlacement)</code>，可以在创建<code>JTabbedPane</code>时设置选项卡的位置，位置参数包括<code>JTabbedPane.TOP</code>、<code>JTabbedPane.BOTTOM</code>、<code>JTabbedPane.LEFT</code>、<code>JTabbedPane.RIGHT</code>。</p>
<p>JTabbedPane 的用法非常简单，只需要创建一个<code>JTabbedPane</code>对象，然后向其中添加多个选项卡，每个选项卡可以包含一个<code>JPanel</code>或其他GUI组件。看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyTabs</span> <span class="keyword">extends</span> <span class="title class_">JFrame</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">MyTabs</span><span class="params">()</span> {</span><br><span class="line">        <span class="type">JTabbedPane</span> <span class="variable">tabbedPane</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTabbedPane</span>(); <span class="comment">// 创建一个 JTabbedPane</span></span><br><span class="line"></span><br><span class="line">        <span class="type">JPanel</span> <span class="variable">panel1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(); <span class="comment">// 创建一个 JPanel</span></span><br><span class="line">        panel1.add(<span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">"This is Panel 1."</span>)); <span class="comment">// 向 JPanel 中添加 JLabel</span></span><br><span class="line">        tabbedPane.addTab(<span class="string">"Tab 1"</span>, panel1); <span class="comment">// 向 JTabbedPane 中添加选项卡</span></span><br><span class="line"></span><br><span class="line">        <span class="type">JPanel</span> <span class="variable">panel2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(); <span class="comment">// 创建另一个 JPanel</span></span><br><span class="line">        panel2.add(<span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">"This is Panel 2."</span>)); <span class="comment">// 向 JPanel 中添加 JLabel</span></span><br><span class="line">        tabbedPane.addTab(<span class="string">"Tab 2"</span>,panel2); <span class="comment">// 向 JTabbedPane 中添加另一个选项卡</span></span><br><span class="line"></span><br><span class="line">        add(tabbedPane); <span class="comment">// 将 JTabbedPane 添加到 JFrame 中</span></span><br><span class="line"></span><br><span class="line">        setTitle(<span class="string">"My Tabs"</span>); <span class="comment">// 设置 JFrame 的标题</span></span><br><span class="line">        setSize(<span class="number">400</span>, <span class="number">300</span>); <span class="comment">// 设置 JFrame 的大小</span></span><br><span class="line">        setLocationRelativeTo(<span class="literal">null</span>); <span class="comment">// 将 JFrame 居中显示</span></span><br><span class="line">        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); <span class="comment">// 设置 JFrame 关闭时的操作为退出程序</span></span><br><span class="line">        setVisible(<span class="literal">true</span>); <span class="comment">// 显示 JFrame</span></span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">MyTabs</span>(); <span class="comment">// 创建并显示一个 MyTabs</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="JScrollPane（滚动窗格）"><a href="#JScrollPane（滚动窗格）" class="headerlink" title="JScrollPane（滚动窗格）"></a>JScrollPane（滚动窗格）</h2><p>滚动窗格只可以添加一个组件，可以把一个组件放到一个滚动窗格中，然后通过滚动条来观看该组件。<code>JTextArea</code>不自带滚动条，因此需要把文本区放到一个滚动窗格中。例如：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JScrollPane</span> <span class="variable">scroll</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JScrollPane</span>(<span class="keyword">new</span> <span class="title class_">JTextArea</span>());</span><br></pre></td></tr></tbody></table></figure>
<h2 id="JSplitPane（拆分窗格）"><a href="#JSplitPane（拆分窗格）" class="headerlink" title="JSplitPane（拆分窗格）"></a>JSplitPane（拆分窗格）</h2><p><code>JSplitPane</code>用于创建可拖动分割面板，允许用户通过拖动分割条来调整两个子组件的大小。它将容器分为两个区域，可以水平或垂直地放置两个子组件。</p>
<p>JSplitPane的构造函数如下：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">JSplitPane(<span class="type">int</span> orientation, Component leftComponent, Component rightComponent)</span><br></pre></td></tr></tbody></table></figure>
<ul>
<li><code>orientation</code>参数指定分割条的方向，可以是<code>JSplitPane.HORIZONTAL_SPLIT</code>（水平分割）或<code>JSplitPane.VERTICAL_SPLIT</code>（垂直分割）。</li>
<li><code>leftComponent</code>和<code>rightComponent</code>参数是要放置在分割条两侧的组件。</li>
</ul>
<p>JSplitPane提供了一些方法来控制和管理分割面板的行为和外观，例如：</p>
<ul>
<li><code>setDividerLocation(double proportionalLocation)</code>：设置分割条的位置，使用比例值（0.0到1.0之间的double）表示位置相对于容器大小的比例。</li>
<li><code>setDividerSize(int newSize)</code>：设置分割条的大小（以像素为单位）。</li>
<li><code>setResizeWeight(double value)</code>：设置两个子组件的调整大小权重，指定它们在分割面板上分配可用空间的比例。</li>
</ul>
<h1 id="使用继承JFrame结构"><a href="#使用继承JFrame结构" class="headerlink" title="使用继承JFrame结构"></a>使用继承JFrame结构</h1><p>在Java GUI编程中，通常让一个类继承<code>JFrame</code>类，并用该类来构建窗口，因为<code>JFrame</code>类提供了一个基本的顶层容器，用于创建和管理GUI应用程序的窗口。</p>
<p>继承<code>JFrame</code>类的好处是：</p>
<ol>
<li>封装窗口逻辑：通过继承<code>JFrame</code>类，可以将窗口相关的逻辑封装在一个类中。这样，你可以将窗口的创建、初始化、布局和事件处理等操作集中在一个类中，使代码更加有组织性和可维护性。</li>
<li>扩展窗口功能：通过继承<code>JFrame</code>类，你可以自定义和扩展窗口的功能。你可以添加自定义的组件、定义窗口的样式和外观，以及实现窗口的自定义行为。</li>
</ol>
<p>下面是一个简单的示例，展示了一个继承<code>JFrame</code>类的自定义窗口类：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.JFrame;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyWindow</span> <span class="keyword">extends</span> <span class="title class_">JFrame</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">MyWindow</span><span class="params">()</span> {</span><br><span class="line">        <span class="comment">// 设置窗口的标题</span></span><br><span class="line">        setTitle(<span class="string">"My Window"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 设置窗口的大小</span></span><br><span class="line">        setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 设置窗口关闭时的操作</span></span><br><span class="line">        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 添加其他组件和布局等操作...</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 显示窗口</span></span><br><span class="line">        setVisible(<span class="literal">true</span>);</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="comment">// 创建自定义窗口对象</span></span><br><span class="line">        <span class="type">MyWindow</span> <span class="variable">window</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyWindow</span>();</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h1 id="布局管理器"><a href="#布局管理器" class="headerlink" title="布局管理器"></a>布局管理器</h1><h2 id="布局管理器介绍"><a href="#布局管理器介绍" class="headerlink" title="布局管理器介绍"></a>布局管理器介绍</h2><p>通常，组件放在容器里，<strong>布局管理器</strong>决定容器中组件的位置和大小。</p>
<p>每个容器都有一个默认的管理器，但可以使用以下语句重新设置：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">panel.setLayout(<span class="keyword">new</span> <span class="title class_">GirldLayout</span>(<span class="number">4</span>,<span class="number">4</span>)); <span class="comment">//其中panel是一个具体的容器</span></span><br></pre></td></tr></tbody></table></figure>
<p>上面的语句会使用<code>GirdLayout</code>类按4行4列摆放组件。往容器中添加组件时，容器的<code>add()</code>方法会把组件和所有位置要求传递给布局管理器。</p>
<p>使用布局管理器的好处是可以实现可伸缩、适应不同屏幕大小和方便的组件排列。以下是几个常用的布局管理器：</p>
<ol>
<li>BorderLayout（边界布局管理器）：<ul>
<li>将容器划分为5个区域：北、南、东、西和中。</li>
<li>默认情况下，组件放置在中心区域，其他区域可以放置边框组件。</li>
<li>每个区域只能放一个组件，如果某个区域已经有组件了还向其中插入组件，则会替换先前的组件。</li>
<li>适用于需要将组件放置在容器的边缘的布局。</li>
</ul>
</li>
<li>FlowLayout（流式布局管理器）：<ul>
<li>以左到右、上到下的顺序依次排列组件。</li>
<li>组件的大小默认为最佳大小，也即刚好能让组件中的内容完整显示。</li>
<li>当组件超过容器的宽度时，会自动换行。</li>
<li>添加到<code>FlowLayout</code>布局的容器中的组件无法调整大小，需要调用<code>setPreferredSize(Dimension preferredSize)</code>设置大小，例如：<code>button.setPreferredSize(new Dimension(20,20))</code>。</li>
<li><code>FlowLayout</code>布局对象调用<code>setLayoutAlignment(int alignment)</code>方法可以设置组件在容器中的对齐方式。alignment参数可以是以下常量之一：<code>FlowLayout.LEFT</code>（默认，左对齐），<code>FlowLayout.CENTER</code>（居中对齐），<code>FlowLayout.RIGHT</code>（右对齐）。</li>
<li>适用于需要按顺序排列组件的布局。</li>
</ul>
</li>
<li>GridLayout（网格布局管理器）：<ul>
<li>将容器划分为网格状的行和列，每个单元格可以放置一个组件。</li>
<li>组件按照指定的行数和列数进行均匀分布。</li>
<li>适用于需要将组件等分放置的布局。</li>
</ul>
</li>
<li>CardLayout（卡片布局管理器）：<ul>
<li>允许在同一个容器中堆叠多个组件，每次只显示其中一个组件。</li>
<li>可以通过切换显示的组件来实现切换界面的效果。</li>
<li>适用于需要实现多个界面切换的布局。</li>
</ul>
</li>
<li>GridBagLayout（网格包布局管理器）：<ul>
<li>具有灵活的布局方式，可以按照组件的需求设置行和列的大小。</li>
<li>支持将组件放置在网格状的单元格中，且每个单元格可以占据多个行和列。</li>
<li>适用于需要精确控制组件位置和大小的布局。</li>
</ul>
</li>
</ol>
<h2 id="如何使用布局管理器"><a href="#如何使用布局管理器" class="headerlink" title="如何使用布局管理器"></a>如何使用布局管理器</h2><p>在使用布局管理器时，通常通过以下步骤实现布局：</p>
<ol>
<li>创建容器对象，如JPanel或JFrame。</li>
<li>创建并添加需要布局的组件到容器中。</li>
<li>创建合适的布局管理器对象，并将其设置给容器。</li>
<li>根据需要，设置布局管理器的属性和约束条件。</li>
<li>最后，将容器添加到父容器中或显示窗口。</li>
</ol>
<h2 id="BorderLayout（边界布局管理器）使用举例"><a href="#BorderLayout（边界布局管理器）使用举例" class="headerlink" title="BorderLayout（边界布局管理器）使用举例"></a>BorderLayout（边界布局管理器）使用举例</h2><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BorderLayoutExample</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">frame</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>(<span class="string">"BorderLayout Example"</span>);</span><br><span class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line"></span><br><span class="line">        <span class="type">JPanel</span> <span class="variable">panel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(<span class="keyword">new</span> <span class="title class_">BorderLayout</span>());</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btnNorth</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"North"</span>);</span><br><span class="line">        panel.add(btnNorth, BorderLayout.NORTH);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btnSouth</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"South"</span>);</span><br><span class="line">        panel.add(btnSouth, BorderLayout.SOUTH);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btnEast</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"East"</span>);</span><br><span class="line">        panel.add(btnEast, BorderLayout.EAST);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btnWest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"West"</span>);</span><br><span class="line">        panel.add(btnWest, BorderLayout.WEST);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btnCenter</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Center"</span>);</span><br><span class="line">        panel.add(btnCenter, BorderLayout.CENTER);</span><br><span class="line"></span><br><span class="line">        frame.add(panel);</span><br><span class="line">        frame.pack();</span><br><span class="line">        frame.setVisible(<span class="literal">true</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="FlowLayout（流式布局管理器）使用举例"><a href="#FlowLayout（流式布局管理器）使用举例" class="headerlink" title="FlowLayout（流式布局管理器）使用举例"></a>FlowLayout（流式布局管理器）使用举例</h2><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FlowLayoutExample</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">frame</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>(<span class="string">"FlowLayout Example"</span>);</span><br><span class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line"></span><br><span class="line">        <span class="type">JPanel</span> <span class="variable">panel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(<span class="keyword">new</span> <span class="title class_">FlowLayout</span>());</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= <span class="number">5</span>; i++) {</span><br><span class="line">            <span class="type">JButton</span> <span class="variable">button</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Button "</span> + i);</span><br><span class="line">            panel.add(button);</span><br><span class="line">        }</span><br><span class="line"></span><br><span class="line">        frame.add(panel);</span><br><span class="line">        frame.pack();</span><br><span class="line">        frame.setVisible(<span class="literal">true</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="GridLayout（网格布局管理器）使用举例"><a href="#GridLayout（网格布局管理器）使用举例" class="headerlink" title="GridLayout（网格布局管理器）使用举例"></a>GridLayout（网格布局管理器）使用举例</h2><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GridLayoutExample</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">frame</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>(<span class="string">"GridLayout Example"</span>);</span><br><span class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line"></span><br><span class="line">        <span class="type">JPanel</span> <span class="variable">panel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(<span class="keyword">new</span> <span class="title class_">GridLayout</span>(<span class="number">3</span>, <span class="number">3</span>));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= <span class="number">9</span>; i++) {</span><br><span class="line">            <span class="type">JButton</span> <span class="variable">button</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Button "</span> + i);</span><br><span class="line">            panel.add(button);</span><br><span class="line">        }</span><br><span class="line"></span><br><span class="line">        frame.add(panel);</span><br><span class="line">        frame.pack();</span><br><span class="line">        frame.setVisible(<span class="literal">true</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="CardLayout（卡片布局管理器）使用举例"><a href="#CardLayout（卡片布局管理器）使用举例" class="headerlink" title="CardLayout（卡片布局管理器）使用举例"></a>CardLayout（卡片布局管理器）使用举例</h2><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CardLayoutExample</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">frame</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>(<span class="string">"CardLayout Example"</span>);</span><br><span class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br><span class="line"></span><br><span class="line">        <span class="type">JPanel</span> <span class="variable">panel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPanel</span>(<span class="keyword">new</span> <span class="title class_">CardLayout</span>());</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btn1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Card 1"</span>);</span><br><span class="line">        panel.add(btn1, <span class="string">"Card 1"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btn2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Card 2"</span>);</span><br><span class="line">        panel.add(btn2, <span class="string">"Card 2"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btn3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Card 3"</span>);</span><br><span class="line">        panel.add(btn3, <span class="string">"Card 3"</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">btnNext</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">"Next"</span>);</span><br><span class="line">        btnNext.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() {</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> {</span><br><span class="line">                <span class="type">CardLayout</span> <span class="variable">cardLayout</span> <span class="operator">=</span> (CardLayout) panel.getLayout();</span><br><span class="line">                cardLayout.next(panel);</span><br><span class="line">            }</span><br><span class="line">        });</span><br><span class="line">        frame.add(btnNext, BorderLayout.SOUTH);</span><br><span class="line">        frame.add(panel, BorderLayout.CENTER);</span><br><span class="line"></span><br><span class="line">        frame.pack();</span><br><span class="line">        frame.setVisible(<span class="literal">true</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h1 id="GUI中常用的类"><a href="#GUI中常用的类" class="headerlink" title="GUI中常用的类"></a>GUI中常用的类</h1><h2 id="Color类"><a href="#Color类" class="headerlink" title="Color类"></a>Color类</h2><p><code>Color</code>类是用于表示颜色的类。它包含了表示各种颜色的预定义常量，同时也可以通过RGB或HSB（色调、饱和度、亮度）值来创建自定义颜色。</p>
<p>以下是<code>Color</code>类的一些常用方法和用法：</p>
<h3 id="使用预定义颜色常量"><a href="#使用预定义颜色常量" class="headerlink" title="使用预定义颜色常量"></a>使用预定义颜色常量</h3><p><code>Color</code>类提供了一些预定义的常量，如<code>RED</code>、<code>GREEN</code>、<code>BLUE</code>等，用于表示基本颜色。看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Color</span> <span class="variable">red</span> <span class="operator">=</span> Color.RED;</span><br><span class="line"><span class="type">Color</span> <span class="variable">green</span> <span class="operator">=</span> Color.GREEN;</span><br><span class="line"><span class="type">Color</span> <span class="variable">blue</span> <span class="operator">=</span> Color.BLUE;</span><br></pre></td></tr></tbody></table></figure>
<h3 id="创建自定义颜色"><a href="#创建自定义颜色" class="headerlink" title="创建自定义颜色"></a>创建自定义颜色</h3><p>可以使用<code>new Color(int r, int g, int b)</code>来创建自定义的RGB颜色，其中<code>r</code>、<code>g</code>和<code>b</code>是0到255之间的整数。看下面的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Color</span> <span class="variable">customColor</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Color</span>(<span class="number">255</span>, <span class="number">128</span>, <span class="number">0</span>); <span class="comment">// 使用RGB值创建自定义颜色</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="Font类"><a href="#Font类" class="headerlink" title="Font类"></a>Font类</h2><p><code>Font</code>类用于表示字体，它提供了控制文本显示样式和特征的功能。可以使用<code>Font</code>类来设置文本的字体、样式、大小和其他属性。</p>
<p>以下是<code>Font</code>类的一些常用方法和用法：</p>
<p>首先，创建一个<code>Font</code>对象.使用<code>Font</code>类的构造函数，您可以创建一个字体对象。参数包括字体名称、样式（粗体、斜体、常规）和字体大小:</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Font</span> <span class="variable">font</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Font</span>(String name, <span class="type">int</span> style, <span class="type">int</span> size);</span><br></pre></td></tr></tbody></table></figure>
<p>其中，<code>style</code>参数包括下列取值：</p>
<ul>
<li><code>Font.PLAIN</code>：常规样式，无粗体或斜体效果。</li>
<li><code>Font.BOLD</code>：粗体样式。</li>
<li><code>Font.ITALIC</code>：斜体样式。</li>
<li><code>Font.BOLD + Font.ITALIC</code>：粗体和斜体样式的组合。</li>
</ul>
<p>在组件中使用字体可以通过：<code>组件.setFont(Font font)</code>来改变组件的默认字体。</p>
<h2 id="ImageIcon类"><a href="#ImageIcon类" class="headerlink" title="ImageIcon类"></a>ImageIcon类</h2><p><code>ImageIcon</code>类是Java Swing中用于表示图像图标的类。它是<code>Icon</code>接口的实现类，可用于在GUI应用程序中显示图像。</p>
<p>以下是<code>ImageIcon</code>类的一些重要方法和用法：</p>
<h3 id="创建ImageIcon对象"><a href="#创建ImageIcon对象" class="headerlink" title="创建ImageIcon对象"></a>创建<code>ImageIcon</code>对象</h3><p>可以使用不同的构造函数创建<code>ImageIcon</code>对象。可以提供图像文件的文件名、图像的URL地址或现有的<code>Image</code>对象：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">ImageIcon</span> <span class="variable">icon</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ImageIcon</span>(String filename);</span><br><span class="line"><span class="type">ImageIcon</span> <span class="variable">icon</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ImageIcon</span>(URL url);</span><br><span class="line"><span class="type">ImageIcon</span> <span class="variable">icon</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ImageIcon</span>(Image image);</span><br></pre></td></tr></tbody></table></figure>
<h3 id="获取图像的宽度和高度"><a href="#获取图像的宽度和高度" class="headerlink" title="获取图像的宽度和高度"></a>获取图像的宽度和高度</h3><p>使用<code>getIconWidth()</code>和<code>getIconHeight()</code>方法，可以获取图像图标的宽度和高度:</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">width</span> <span class="operator">=</span> icon.getIconWidth();</span><br><span class="line"><span class="type">int</span> <span class="variable">height</span> <span class="operator">=</span> icon.getIconHeight();</span><br></pre></td></tr></tbody></table></figure>
<h3 id="设置图标的描述文本"><a href="#设置图标的描述文本" class="headerlink" title="设置图标的描述文本"></a>设置图标的描述文本</h3><p>使用<code>setDescription()</code>方法，可以为图标设置描述文本。这对于在鼠标悬停时显示工具提示信息很有用:</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">icon.setDescription(String description);</span><br></pre></td></tr></tbody></table></figure>
<h3 id="获取图标的图像"><a href="#获取图标的图像" class="headerlink" title="获取图标的图像"></a>获取图标的图像</h3><p> 使用<code>getImage()</code>方法，可以获取<code>ImageIcon</code>对象中包含的图像:</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Image</span> <span class="variable">image</span> <span class="operator">=</span> icon.getImage();</span><br></pre></td></tr></tbody></table></figure>
<h3 id="IconImage类在GUI编程中的应用"><a href="#IconImage类在GUI编程中的应用" class="headerlink" title="IconImage类在GUI编程中的应用"></a><code>IconImage</code>类在GUI编程中的应用</h3><p>在GUI编程中，可以将<code>ImageIcon</code>应用于许多组件和场景，例如：</p>
<ul>
<li><p>将图标显示在<code>JLabel</code>中：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JLabel</span> <span class="variable">label</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>();</span><br><span class="line">label.setIcon(icon);</span><br></pre></td></tr></tbody></table></figure>
</li>
<li><p>在按钮上显示图标：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JButton</span> <span class="variable">button</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>();</span><br><span class="line">button.setIcon(icon);</span><br></pre></td></tr></tbody></table></figure>
</li>
<li><p>在菜单项上显示图标：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">JMenuItem</span> <span class="variable">menuItem</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JMenuItem</span>();</span><br><span class="line">menuItem.setIcon(icon);</span><br></pre></td></tr></tbody></table></figure>
</li>
<li><p>在对话框中显示图标：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">JOptionPane.showMessageDialog(<span class="literal">null</span>, <span class="string">"Message"</span>, <span class="string">"Title"</span>, JOptionPane.INFORMATION_MESSAGE, icon);</span><br></pre></td></tr></tbody></table></figure>
</li>
</ul>
<p>通过将<code>ImageIcon</code>对象与适当的Swing组件结合使用，可以在GUI应用程序中显示图像图标，并为用户提供更丰富的视觉体验。</p>
<h1 id="事件处理"><a href="#事件处理" class="headerlink" title="事件处理"></a>事件处理</h1><p>在Java中，事件处理是指对用户交互或系统事件作出响应的机制。它允许程序捕获和处理事件，例如按钮点击、鼠标移动、键盘按下等。</p>
<p>Java事件处理基于事件和监听器。以下是事件处理的基本概念：</p>
<ol>
<li>事件（Event）：<strong>事件是发生在应用程序中的特定操作或状态变化</strong>。例如，按钮被点击、鼠标移动、键盘按下等都是事件。每个事件都与特定的类关联，例如<code>ActionEvent</code>、<code>MouseEven</code>t、<code>KeyEvent</code>等。</li>
<li>事件源（Event Source）：<strong>事件源是事件发生的对象或组件</strong>。它是可以触发事件的实体，例如按钮、文本框、鼠标等。事件源类通常是派生自<code>AWT</code>或<code>Swing</code>类库中的组件类。</li>
<li>监听器（Listener）：<strong>监听器是负责接收和处理事件的对象</strong>。它实现了事件监听器接口，该接口定义了处理事件的方法。<strong>监听器会注册到事件源上，以便在事件发生时被通知</strong>。<strong>每种类型的事件都有对应的监听器接口</strong>，例如<code>ActionListener</code>、<code>MouseListener</code>、<code>KeyListener</code>等。</li>
<li>注册监听器（Register Listener）：在Java中，<strong>需要将监听器注册到事件源上</strong>，以便在事件发生时能够接收到通知。通常使用事件源的<code>addXXXListener()</code>方法进行注册，其中<code>XXX</code>是特定事件类型的名称。</li>
<li>事件处理方法（Event Handling Method）：事件处理方法是监听器接口中定义的方法，用于处理特定类型的事件。当事件发生时，事件源会调用监听器的事件处理方法，将事件对象作为参数传递给方法，以便处理事件。</li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Java%E5%9F%BA%E7%A1%80-GUI/" rel="tag"># -Java基础 -GUI</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/05/18/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%8E%9F%E7%90%86%EF%BC%9A%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1/" rel="prev" title="数据库原理：数据库设计">
                  <i class="fa fa-chevron-left"></i> 数据库原理：数据库设计
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/05/21/Java%EF%BC%9AIO%E6%B5%81/" rel="next" title="Java：IO流">
                  Java：IO流 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
